package com.hot100;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class h046 {
    public static void main(String[] args) {
        int[] nums = {1,2,3};
        List<List<Integer>> permute = permute(nums);
        permute.forEach(p -> System.out.println(p));
    }
    public static List<List<Integer>> permute(int[] nums) {
        LinkedList<Integer> list = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        boolean[] is = new boolean[nums.length];
        recur(nums,list,res,is,0);
        return res;
    }

    public static void recur(int[] nums,LinkedList<Integer> list,List<List<Integer>> res,boolean[] is,int index){
         if (index == nums.length){
             res.add(new LinkedList<>(list));
             return;
         }

         for (int i = 0;i<nums.length;i++){
             if (is[i]) continue;
             list.add(nums[i]);
             is[i] = true;
             recur(nums,list,res,is,index+1);
             list.removeLast();
             is[i] = false;
         }
    }
}
